/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package radiostation;

import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import model.Playlist;
import model.Song;

/**
 *
 * @author stamatelopoulos
 */
public class EditPlayListForm extends javax.swing.JFrame {

    /**
     * Creates new form EditPlayListForm
     */
    public static SimpleDateFormat dateFormater = new SimpleDateFormat("dd/MM/yyyy");
    int availableSongRow;
    int selectedSongRow;
        
    Playlist playlist;
    Song song;

    public EditPlayListForm(Playlist pl, boolean readOnly) {    

        em = DBManager.em;
        this.readOnly = readOnly;
        this.pl = pl;
        if ( !(em.getTransaction().isActive()) ){
            em.getTransaction().begin();
        }
        initComponents();
        DBManager.centerOnScreen(this);
        this.AvailableSongTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.SelectedSongTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        TableCellRenderer rendererFromHeader = AvailableSongTable.getTableHeader().getDefaultRenderer();
        JLabel Id = (JLabel)rendererFromHeader;
        Id.setHorizontalAlignment(JLabel.CENTER);
        
        TableCellRenderer rendererFromHeader2 = SelectedSongTable.getTableHeader().getDefaultRenderer();
        JLabel firstName = (JLabel)rendererFromHeader2;
        firstName.setHorizontalAlignment(JLabel.CENTER);

        JTableHeader header = AvailableSongTable.getTableHeader();
        header.setForeground(Color.red);
        
        JTableHeader header2 = SelectedSongTable.getTableHeader();
        header2.setForeground(Color.red);

        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        AvailableSongTable.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);
        SelectedSongTable.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);
        
        if (readOnly == false){ //add new playlist
            System.out.println("new playlist");
            SelectedSongList.clear(); 
        }
        else { //edit existing playlist
            System.out.println("edit existing playlist");
            jDateChooser1.setDate(playlist1.getCreationdate());
            AvailableSongList.clear();
            AvailableSongList.addAll(SongQuery.getResultList());
            SelectedSongList.clear();
            for (Song song : pl.getSongList()){
                SelectedSongList.add(song);
            }
        }
    }
    
    private void manageButtons() {
        availableSongRow = AvailableSongTable.getSelectedRow();
        selectedSongRow = SelectedSongTable.getSelectedRow();
        insButton.setEnabled(availableSongRow >= 0);
        delButton.setEnabled(selectedSongRow >= 0);
    }
    
    public EditPlayListForm() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        SongQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("select s from Song s order by s.title");
        AvailableSongList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(SongQuery.getResultList());
        SelectedSongList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(SongQuery.getResultList());
        playlist1 = pl;
        artist1 = new model.Artist();
        musicgroup1 = new model.Musicgroup();
        album1 = new model.Album();
        ShowAll = java.beans.Beans.isDesignTime() ? null : em.createQuery("select s from Song s order by s.title");
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        kodField = new javax.swing.JTextField();
        NameField = new javax.swing.JLabel();
        NameTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        DescriptionTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        SearchTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        AvailableSongTable = new javax.swing.JTable();
        insButton = new javax.swing.JButton();
        delButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        SelectedSongTable = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        showAll = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        SaveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Διαχείριση Λίστας Τραγουδιών");

        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setText("Στοιχεία Λίστας Τραγουδιών:");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("Κωδικός:");
        jLabel2.setEnabled(false);

        kodField.setEditable(false);
        kodField.setForeground(new java.awt.Color(204, 204, 204));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, playlist1, org.jdesktop.beansbinding.ELProperty.create("${id}"), kodField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        NameField.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        NameField.setText("Ονομασία:");

        NameTextField.setToolTipText("Πληκτρολογείστε την Ονομασία της Λίστας Τραγουδιών");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, playlist1, org.jdesktop.beansbinding.ELProperty.create("${name}"), NameTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Περιγραφή:");

        DescriptionTextField.setToolTipText("Πληκτρολογείστε την Περιγραφή της Λίστας Τραγουδιών");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, playlist1, org.jdesktop.beansbinding.ELProperty.create("${description}"), DescriptionTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Ημερ. Δημιουργίας:");

        jDateChooser1.setToolTipText("Επιλέξτε την Ημερομηνία Δημιουργίας");
        jDateChooser1.setDateFormatString("d MMMM yyyy");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(kodField, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(NameField))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(NameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(108, 108, 108))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(kodField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NameField)
                    .addComponent(NameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(DescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4))
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        SearchTextField.setToolTipText("Δώστε Τραγούδι ή Καλλιτέχνη ή Συγκρότημα");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/magnifier-arrow-icon-icon.png"))); // NOI18N
        jButton1.setText("Αναζήτηση");
        jButton1.setToolTipText("Πατήστε Αναζήτηση με συγκεκριμένα κριτήρια");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel5.setForeground(new java.awt.Color(0, 51, 255));
        jLabel5.setText("Διαθέσιμα Τραγούδια:");

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, AvailableSongList, AvailableSongTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Τίτλος");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${falbumId.fgroupId}${falbumId.fartistId}"));
        columnBinding.setColumnName("Καλλιτέχνης/Συγκρότημα");
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duration}"));
        columnBinding.setColumnName("Διάρκεια {sec}");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${falbumId.fgroupId}"));
        columnBinding.setColumnName("");
        columnBinding.setColumnClass(model.Musicgroup.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        AvailableSongTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AvailableSongTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(AvailableSongTable);
        if (AvailableSongTable.getColumnModel().getColumnCount() > 0) {
            AvailableSongTable.getColumnModel().getColumn(3).setMinWidth(0);
            AvailableSongTable.getColumnModel().getColumn(3).setPreferredWidth(0);
            AvailableSongTable.getColumnModel().getColumn(3).setMaxWidth(0);
        }

        insButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/next.png"))); // NOI18N
        insButton.setText("Εισαγωγή");
        insButton.setToolTipText("Πατήστε για εισαγωγή τραγουδιού στη Λίστα");
        insButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insButtonActionPerformed(evt);
            }
        });

        delButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/previous.png"))); // NOI18N
        delButton.setText("Διαγραφή");
        delButton.setToolTipText("Πατήστε για αφαίρεση του τραγουδιού από τη Λίστα");
        delButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delButtonActionPerformed(evt);
            }
        });

        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, SelectedSongList, SelectedSongTable);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Τίτλος");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${falbumId.fgroupId}${falbumId.fartistId}"));
        columnBinding.setColumnName("Καλλιτέχνης/Συγκρότημα");
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duration}"));
        columnBinding.setColumnName("Διάρκεια {sec}");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${falbumId.fgroupId}"));
        columnBinding.setColumnName("");
        columnBinding.setColumnClass(model.Musicgroup.class);
        columnBinding.setEditable(false);
        jTableBinding.setSourceNullValue(SelectedSongList);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        SelectedSongTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SelectedSongTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(SelectedSongTable);
        if (SelectedSongTable.getColumnModel().getColumnCount() > 0) {
            SelectedSongTable.getColumnModel().getColumn(3).setMinWidth(0);
            SelectedSongTable.getColumnModel().getColumn(3).setPreferredWidth(0);
            SelectedSongTable.getColumnModel().getColumn(3).setMaxWidth(0);
        }

        jLabel6.setForeground(new java.awt.Color(0, 51, 255));
        jLabel6.setText("Τραγούδια Λίστας:");

        jLabel8.setForeground(new java.awt.Color(0, 102, 102));
        jLabel8.setText("Κριτήρια Αναζήτησης Τραγουδιών:");

        showAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/magnifier-pencil-icon-icon.png"))); // NOI18N
        showAll.setText("Εμφάνιση Όλων");
        showAll.setToolTipText("Πατήστε για εμφάνιση όλων των Τραγουδιών");
        showAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 441, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(delButton)
                                    .addComponent(insButton)))
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(SearchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(showAll)))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel8))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SearchTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(showAll))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(insButton)
                        .addGap(27, 27, 27)
                        .addComponent(delButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        CancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        CancelButton.setText("Ακύρωση");
        CancelButton.setToolTipText("Πατήστε για ακύρωση και επιστροφή στη Φόρμα Πίνακας Λίστας Τραγουδιών");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        SaveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        SaveButton.setText("Αποθήκευση");
        SaveButton.setToolTipText("Πατήστε για αποθήκευση των αλλαγών και επιστροφή στη Φόρμα");
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(CancelButton)
                .addGap(18, 18, 18)
                .addComponent(SaveButton)
                .addGap(41, 41, 41))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelButton)
                    .addComponent(SaveButton))
                .addGap(21, 21, 21))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        // TODO add your handling code here:
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, false);
        for (WindowListener l : this.getWindowListeners()){
            l.windowClosed(we);
        }
        this.setVisible(false); 
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
        // TODO add your handling code here:
        if (SelectedSongList.isEmpty()){
            JOptionPane.showMessageDialog(null, "Προσοχή δεν έχετε επιλέξει τραγούδια για τη Λίστα Τραγουδιών!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE); 
            return;
        }        

        int duration = 0;
        String searchItem="";
        int rows = SelectedSongTable.getRowCount();
        
        for (Song song : SelectedSongList){
            duration+=song.getDuration();
            System.out.println("Duration: " + duration);
        }

        for (int i=0;i<SelectedSongList.size();i++){
            searchItem = SelectedSongList.get(i).getTitle(); ;
            for (int j=i+1;j<SelectedSongList.size();j++){
                if (SelectedSongList.get(j).getTitle() == searchItem){ 
                   JOptionPane.showMessageDialog(null, "Προσοχή το Τραγούδι " + SelectedSongList.get(j).getTitle() + " υπάρχει ήδη στη Λίστα!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);  
                   return;
                }
            }
        }
                
        if (NameTextField.getText().isEmpty() || DescriptionTextField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Προσοχή υπάρχουν υποχρεωτικά πεδία που δεν έχουν συμπληρωθεί!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);
            return;
        }else if (duration < 1800){ //πρέπει να γινει ελεγχος για διαρκεια λιστας
            JOptionPane.showMessageDialog(null, "Προσοχή η Λίστα Τραγουδιών πρέπει να έχει τουλάχιστον 30' διάρκεια!  Τρέχουσα τιμή: [" + duration/60 + "']", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE);    
            return;
        } else { 
         
        playlist1.setCreationdate(jDateChooser1.getDate());
        
        for (Song song : pl.getSongList()){
             song = em.merge(song);
             if (!SelectedSongList.contains(song)){
                 song.getPlaylistList().remove(pl);
             }
        }
        pl.getSongList().retainAll(SelectedSongList);
        for (Song song : SelectedSongList){
            song = em.merge(song);
            if (!pl.getSongList().contains(song)){
                pl.getSongList().add(song);
                song.getPlaylistList().add(pl);
            }
        }  
            
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, true);
        for (WindowListener l : this.getWindowListeners()){
            l.windowClosed(we);
        }
        this.setVisible(false);
        }
    }//GEN-LAST:event_SaveButtonActionPerformed

    private void showResults() {

        if (!SearchTextField.getText().trim().equals("")){
            SongQuery = em.createQuery("SELECT s FROM Song s where s.title LIKE :title UNION "
                        + "SELECT s FROM Song s where s.falbumId.fartistId.artisticname LIKE :artisticname UNION "
                        + "SELECT s FROM Song s where s.falbumId.fgroupId.name LIKE :name")
                        .setParameter("title", '%' + SearchTextField.getText() + '%')
                        .setParameter("artisticname", '%' + SearchTextField.getText() + '%')
                        .setParameter("name", '%' + SearchTextField.getText() + '%');
        }else {
           JOptionPane.showMessageDialog(null, "Προσοχή πρέπει να εισάγετε κριτήρια για Αναζήτηση!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE); 
           return;
        }

        java.util.List data = SongQuery.getResultList();
        
        for (Object entity : data) {
            em.refresh(entity);
        }
        
        if (data.isEmpty()) { 
           JOptionPane.showMessageDialog(null, "Δεν βρέθηκαν Τραγούδια. Δώστε καλύτερα κριτήρια Αναζήτησης!", "ΜΗΝΥΜΑ ΛΑΘΟΥΣ", JOptionPane.ERROR_MESSAGE); 
        }else{
            AvailableSongList.clear();
            AvailableSongList.addAll(SongQuery.getResultList());
        }
    }
    
    private void AvailableSongTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AvailableSongTableMouseClicked
        // TODO add your handling code here:
        SelectedSongTable.clearSelection();
        delButton.setEnabled(false);
        manageButtons();
    }//GEN-LAST:event_AvailableSongTableMouseClicked

    private void SelectedSongTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SelectedSongTableMouseClicked
        // TODO add your handling code here:
        AvailableSongTable.clearSelection();
        insButton.setEnabled(false);
        manageButtons();    
    }//GEN-LAST:event_SelectedSongTableMouseClicked

    private void insButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insButtonActionPerformed
        // TODO add your handling code here:
        //προσθήκη Τραγουδιού στη Λίστα
        song = AvailableSongList.get(AvailableSongTable.convertRowIndexToModel(availableSongRow));
        AvailableSongList.remove(song);
        SelectedSongList.add(SelectedSongList.size(),song);
        insButton.setEnabled(false);
    }//GEN-LAST:event_insButtonActionPerformed

    private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButtonActionPerformed
        // TODO add your handling code here:
        //Αφαίρεση Τραγουδιού από τη Λίστα
        song = SelectedSongList.get(SelectedSongTable.convertRowIndexToModel(selectedSongRow));
        SelectedSongList.remove(song);
        AvailableSongList.add(AvailableSongList.size(),song);
        delButton.setEnabled(false);
    }//GEN-LAST:event_delButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        showResults();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void showAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllActionPerformed
        // TODO add your handling code here:
        ShowAll = em.createQuery("SELECT s FROM Song s ORDER BY s.title");
        java.util.List data = ShowAll.getResultList();
        
        for (Object entity : data) {
            em.refresh(entity);
        }
        
        AvailableSongList.clear();
        AvailableSongList.addAll(ShowAll.getResultList());

    }//GEN-LAST:event_showAllActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<model.Song> AvailableSongList;
    private javax.swing.JTable AvailableSongTable;
    private javax.swing.JButton CancelButton;
    private javax.swing.JTextField DescriptionTextField;
    private javax.swing.JLabel NameField;
    private javax.swing.JTextField NameTextField;
    private javax.swing.JButton SaveButton;
    private javax.swing.JTextField SearchTextField;
    private java.util.List<model.Song> SelectedSongList;
    private javax.swing.JTable SelectedSongTable;
    private javax.persistence.Query ShowAll;
    private javax.persistence.Query SongQuery;
    private model.Album album1;
    private model.Artist artist1;
    private javax.swing.JButton delButton;
    private javax.swing.JButton insButton;
    private javax.swing.JButton jButton1;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField kodField;
    private model.Musicgroup musicgroup1;
    private model.Playlist playlist1;
    private javax.swing.JButton showAll;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    private EntityManager em;
    private model.Playlist pl;
    JFrame thisFrame;
    boolean readOnly;
}
